package com.sunday.fangdaifu.activity.task;

import android.os.Bundle;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.sunday.common.base.BaseActivity;
import com.sunday.common.utils.ToastUtils;
import com.sunday.common.widgets.ActionSheet;
import com.sunday.fangdaifu.R;
import com.sunday.fangdaifu.base.MyApplication;
import com.sunday.fangdaifu.service.LocationService;
import com.sunday.fangdaifu.widgets.SelectMapWindow;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 刘涛 on 2017/7/20.
 */

public class MapActivity extends BaseActivity {


    @Bind(R.id.title_view)
    TextView titleView;

    @Bind(R.id.txt_addr)
    TextView txtAddr;
    @Bind(R.id.btn_select)
    ImageView btnSelect;
    private String longitude, latitude,addr;

    private MapView mMapView = null;
    BaiduMap mBaiduMap;
    private LocationService locationService;
    LocationClient mClient;
    LatLng point;
    MapStatusUpdate center;
    private List<BitmapDescriptor> bitmapDescriptors=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        ButterKnife.bind(this);
        titleView.setText("巡检地址");
        mMapView = (MapView) findViewById(R.id.map_view);
        longitude=getIntent().getStringExtra("longitude");
        latitude=getIntent().getStringExtra("latitude");
        addr=getIntent().getStringExtra("addr");
        txtAddr.setText(addr);
        mBaiduMap = mMapView.getMap();
        mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
        mBaiduMap.setMapStatus(MapStatusUpdateFactory.zoomTo(15));
        locationService = ((MyApplication) getApplication()).locationService;
        LocationClientOption mOption = locationService.getDefaultLocationClientOption();
        mOption.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
        mOption.setCoorType("bd09ll");
        mClient = new LocationClient(this);
        mClient.setLocOption(mOption);
        mClient.registerLocationListener(listener);
        mClient.start();
        initEndingPosition();


    }
    private void initEndingPosition(){
        LatLng point = new LatLng(Double.parseDouble(longitude), Double.parseDouble(latitude));
        BitmapDescriptor bdA = BitmapDescriptorFactory.fromResource(R.mipmap.icon_en);
        bitmapDescriptors.add(bdA);
        OverlayOptions option = new MarkerOptions().position(point).icon(bdA);
        mBaiduMap.addOverlay(option);
    }


    /***
     * 定位结果回调，在此方法中处理定位结果
     */
    private String oriLatit, oriLongit;
    BDLocationListener listener = new BDLocationListener() {

        @Override
        public void onReceiveLocation(BDLocation location) {
            // TODO Auto-generated method stub
            if (location != null && location.getLocType() != BDLocation.TypeServerError) {
                Log.i(TAG, "定位成功");
                mBaiduMap.clear();
                final LatLng point = new LatLng(location.getLatitude(), location.getLongitude());
                oriLatit= String.valueOf(location.getLatitude());
                oriLongit= String.valueOf(location.getLongitude());
                BitmapDescriptor bd = BitmapDescriptorFactory.fromResource(R.mipmap.icon_gcoding);
                bitmapDescriptors.add(bd);
                OverlayOptions option = new MarkerOptions().position(point).icon(bd);
                // 在地图上添加Marker，并显示
               mBaiduMap.addOverlay(option);
                mBaiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(point));
                initEndingPosition();
            } else {
                ToastUtils.showToast(mContext, "定位失败，请开启位置服务后重新定位");
            }

        }

        @Override
        public void onConnectHotSpotMessage(String s, int i) {

        }
    };


    @OnClick(R.id.btn_select)
    void selectMap(){
        SelectMapWindow window=new SelectMapWindow(mContext,oriLatit,oriLongit,latitude,longitude);
        window.showPopupWindow(btnSelect);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mMapView.onDestroy();
        mBaiduMap.setMyLocationEnabled(false);
        mClient.stop();
        locationService.unregisterListener(listener);
        for(BitmapDescriptor item:bitmapDescriptors){
            if(item.getBitmap()!=null&&!item.getBitmap().isRecycled()){
                item.recycle();
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        mMapView.onResume();


    }

    @Override
    protected void onPause() {
        super.onPause();
        mMapView.onPause();
    }
}
